package bit.lotterysystem.service.activityStatus.operater;

import bit.lotterysystem.dao.dataobject.ActivityPrizeDO;
import bit.lotterysystem.dao.mapper.ActivityMapper;
import bit.lotterysystem.dao.mapper.ActivityPrizeMapper;
import bit.lotterysystem.service.dto.ConvertActivityStatusDTO;
import bit.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperater extends AbstractActivityOperater{
    @Autowired
    private final ActivityMapper activityMapper;

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    public PrizeOperater(ActivityMapper activityMapper) {
        super();
        this.activityMapper = activityMapper;
    }

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId=convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus=convertActivityStatusDTO.getTargetPrizeStatus();
        if(null==prizeId
                || null==targetPrizeStatus){
            return false;
        }
        ActivityPrizeDO activityPrizeDO=activityPrizeMapper.selectByAPId(activityId,prizeId);
        if(null==activityPrizeDO){
            return false;
        }
        //判断当前奖品状态和目标状态是否一致
        if(targetPrizeStatus.name().equalsIgnoreCase(activityPrizeDO.getStatus())){
            return false;
        }
        return true;
    }

    @Override
    public boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId=convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus=convertActivityStatusDTO.getTargetPrizeStatus();
        try{
            activityPrizeMapper.updateStatus(activityId,prizeId,targetPrizeStatus.name());
            return true;
        }catch(Exception e){
            return false;
        }
    }
}
